/**
 * Created by yx on 2017/7/6.
 */
$('.control').on('click',function () {
   $(this).hide();
   $('.cover').hide();
   $('.myVideo').get(0).play()
});

$("video").trigger("play");                 //for auto play
$("video").addClass('pause');           //for check pause or play add a class
$('video').click(function() {
    if ($(this).hasClass('pause')) {
        $("video").trigger("play");
        $(this).removeClass('pause');
        $(this).addClass('play');
    } else {
        $("video").trigger("pause");
        $(this).removeClass('play');
        $(this).addClass('pause');
    }
});

//点击展示所有评论
var flag = true;
$(document).on('click','.commentCount',function () {
    if(flag) {
        $(this).parent().css('height','auto');
        $(this).siblings(".subComment").css('display','block');
        flag = false;
    } else {
        $(this).parent().css('height','70px');
        $(this).siblings(".subComment").css('display','none');
        $(this).parent().children(".subComment:first-child").css('display','block');
        flag = true;
    }
});

getVideo();
getComment();

function ajax(url, param, type) {
    // 利用了jquery延迟对象回调的方式对ajax封装，使用done()，fail()，always()等方法进行链式回调操作
    return $.ajax({
        url: url,
        data: param || {},
        type: type || 'GET',
        dataType: "json"
    });
};

function getVideo() {
    $.ajax({
        url: '/news/posts/getPostsDtlById?postId=4',
        type: 'GET',
        dataType: "json",
        headers: {"Authorization":"eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MDE5ODkyNjYsInVzZXJJZCI6IjE1d2F5NXh4bmRjM29ld3VvZ2N6In0.K41L7hgqsPR-GPSN1jNDd4WWJ2BlEifAHSYvO3teAHY"},
        success: function (res) {
            console.log(res);
            var data = res;
            if(data.success) {
                $('.myVideo').find('source').attr('src',data.data.newsUrl);
                // $('.cover').find('img').attr('src',data.data.listLayout.layout_imgs);
            }else {
                  alert(data.message)
            };
        },
        error: function (err) {

        }
    })
}

function getComment() {
    $.ajax({
        url: '/community/forumcomment/getForumcommentByForumMsgId?forumMsgId=06b3ae9ac00e4c5c8377d5c601dbc2d1',
        type: 'GET',
        dataType: "json",
        success: function (res) {
            console.log(res);
            var data = res;
            if(data.success) {
                //如果有评论
                if(data.data.length>0){
                    $(".comment").empty();
                    var comment = data.data;
                    for(var i = 0;i < comment.length; i++){
                        var temp = $("#commentTemp").clone().html();
                        //评论用户昵称
                        var html = temp.replace("用户昵称01", comment[i].userNickname);
                        //评论时间
                        html = html.replace("10分钟前", timeChange(comment[i].createDate));
                        //评论内容
                        html = html.replace("content1", comment[i].content);
                        //点赞数
                        html = html.replace("likeNum", comment[i].likesCount == null?0:comment[i].likesCount);
                        //评论图片
                        if(comment[i].commentPicUrl) {
                            html = html.replace("defaultImg",comment[i].commentPicUrl);
                        } else {
                            html = html.replace("commentImg","commentImg hidden");
                        }
                        //如果存在子评论
                        if(comment[i].listForumcomment.length>0){
                            var commentList = comment[i].listForumcomment;
                            var text = '';
                            for(var j = 0;  j< commentList.length; j++) {
                                text += "<div class='subComment'><p class='title'>"+commentList[j].userNickname+"回复"+comment[i].userNickname+"</p><p class='text'>"+commentList[j].content+"</p></div>";
                            };
                            text += "<p class='commentCount'>共"+commentList.length+"条回复<i class='icon iconfont1 icon-xiangyou'></i></p>"
                            html = html.replace("回复",text);
                        }else {
                            html = html.replace("reply","reply hidden");
                            // $('.reply').remove();
                        };
                        $('.comment').append(html);
                        var height = $(".commentBody").eq(i).find(".subComment:first-child").height();
                        if(height) {
                            $(".commentBody").eq(i).find(".reply").height(height+30)
                        };
                    };
                }else{
                    $('.comment').empty();
                    $('.comment').append("<div class='NoComment'><img src='image/empty.jpg'></div><p class='NoCommetText'>还没有人评论，有些寂寞</p>");
                }
            }else {
                alert(data.message)
            };
        },
        error: function (err) {

        }
    })
}

function timeChange(time) {
    var timestamp1 = Date.parse(new Date());
    var timestamp2 = Date.parse(time.replace(/-/g, "/"));
    if((timestamp1 - timestamp2) > 86400) {
        return time;
    }else if( 86400 > (timestamp1 - timestamp2) > 3600) {
        return parseInt((timestamp1 - timestamp2)/3600)+"小时前"
    }else {
        return parseInt((timestamp1 - timestamp2)/60)+"分钟前"
    }
}